<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('MessageHelper');
$MessageHelper = new MessageHelper();

$this->LoadHelper('BankHelper');
$BankHelper = new BankHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);

$dp_code = $member_info['u_dpcode'];
$this->LoadResurces('oldapi/class.carloan');
$oldapi = new Carloan();

$sign = $oldapi->sign(array('code' => $dp_code));
$api_baseurl = OLD_SITE_URL . '/OrderDetails/index/?code=' . $dp_code . '&sign=' . $sign['sign'];
if (empty($this->Param['option'])) {
	$ajax = $this->Param['ajax'];
	$page = isset($this->Param['page'])? $this->Param['page'] : 1;
	$rp = isset($this->Param['rp'])? $this->Param['rp'] : PAGE_SIZE;
	if($ajax){
		$where = array(
			'`o_status` IN(?)' => array('初审提交','审核中'),
			'`o_videoaudit_status` IN(?)' => array('审核中','未审核','暂停中')
		);
		//$where['`ot_type` = ?'] = '视频审核';
        if (empty($this->Param['o_keyword'])) {
            if ($member_info['u_role'] == 10) {//视频审核员
                $where['`o_videoaudit_perch` IN (?)'] = array($member_info['u_id'], 0);
            }
        }else{
            $where['`o_videoaudit_status` IN(?)'] = array('审核中', '未审核','暂停中');
        }
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info, 'order', array('`o_finishe_time` DESC','`o_first_time` ASC'));
        $json_data = array('total' => $data['count'], 'rows' => array());
        $mode = Config::GetCommon();
        if (!empty($data['all']))
        	foreach ($data['all'] AS $rowNum => $row) {
        		//贷款金额
        		$order_car = $OrderHelper->orderCar($row['o_id']);
                $row['ot_loan_limit'] = $order_car['ot_loan_limit'];
                //贷款银行
                $bank_row = $BankHelper->bankRow($row['o_bank_id']);
                $row['o_bank_abb'] = $bank_row['bk_abbreviation'];
                $row['o_pre_modetext'] = $mode['mode'][$row['o_pre_mode']]['mode_text'];
                $row['o_sell_time'] = empty($row['o_sell_time']) ? '' : date('Y-m-d H:i:s', $row['o_sell_time']);
                $row['o_search_time'] = empty($row['o_search_time']) ? '' : date('Y-m-d H:i:s', $row['o_search_time']);
                $row['o_first_time'] = empty($row['o_first_time']) ? '' : date('Y-m-d H:i:s', $row['o_first_time']);
                $o_perch = $MemberHelper->getMemberRow($row['o_videoaudit_perch']);
                $row['o_videoaudit_perch'] = $o_perch['u_realname']; //占位人员
                if($row['o_oldsys'] == 1){
                    $sign = $oldapi->sign(array('code' => $dp_code));
                    $row['info_url'] = $api_baseurl.'&id='.$row['o_number'];
                }else{
                    $row['info_url'] = '/index/index/option/info?oid='.$row['o_id'];
                }
                $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
	}else{
		echo $this->GetView('order_fvolist.php');
	}
} else {
    switch ($this->Param['option']) {
        case 'fvoinfo':
        	$oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
        	$order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            //检测是否有申请修改项
            $upcount=$OrderHelper->orderuping($oid);
            if($upcount>0){
                echo "<script>alert('当前订单有未处理的视频修改申请');</script>";
            }

            //占位判断
            $result = $OrderHelper->videoperJudge($oid, $member_info);
            if(!$result['success']){
                ErrorMsg::Debug($result['msg']);
            }
            $order = $OrderHelper->orderInfo($oid);
            $order['audit_draft'] = $OrderHelper->orderAuditfilter($order['audit_all']['draft'], '视频审核');
            $this->Assign('order', $order);
            echo $this->GetView('order_fvoinfo.php');
            break;
        case 'save':
        	$oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $save_type = empty($this->Param['savetype']) ? 'draft' : $this->Param['savetype'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $upcount=$OrderHelper->orderuping($oid);
            if($upcount>0){
                ErrorMsg::Debug('当前订单有未处理的视频修改申请');
            }
            if($save_type == 'draft'){
                $this->Param['draft'] = true;
            } else {
                $this->Param['draft'] = false;
            }
            $this->Param['a_uid'] = $this->UserConfig['Id'];
            $result = $OrderHelper->videoAudit($this->Param, $oid);
            if ($result['success']) {
                if($this->Param['a_results']=='不予办理'&& $save_type=='save'){
                    $result1 = $OrderHelper->orderupUpdate(array(
                        'up_status' => '作废'
                    ),'',$oid);
                }

                if ($save_type != 'draft') {
                    $MemberHelper->saveLog($this->UserConfig, '终审审核', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                    $suspendMsg = $OrderHelper->suspendMsg($oid);
                    if($this->Param['a_results']=='可以办理'){
                    	if(!empty($suspendMsg))  $MessageHelper->sendSingle($suspendMsg);
                    }
                    if(!empty($suspendMsg))  $MessageHelper->sendSingle($suspendMsg);
                }
                ErrorMsg::Debug($save_type == 'draft' ? '保存成功' : '审核成功', true);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
        	break;
       	case 'chkout': //订单签出
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) {
                ErrorMsg::Debug('无权操作');
            }
            $result = $OrderHelper->orderChkout($oid, '视频审', array('uid' => $this->UserConfig['Id'], 'uname' => $member_info['u_realname']));
            if ($result['success']) {
                $MemberHelper->saveLog($this->UserConfig, '视频审签出', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug('保存成功', true);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        /*case 'back': //订单退回
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) {
                ErrorMsg::Debug('无权操作');
            }
            $this->Assign('oid', $oid);
            echo $this->GetView('order_fvoback.php');
            	break;
        case 'backsave':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $status = empty($this->Param['status']) ? ErrorMsg::Debug('请选择退回状态') : $this->Param['status'];
            $remarks = $this->Param['remarks'];
            $v_type = 'fvooprate';
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) {
                ErrorMsg::Debug('无权操作');
            }
            $result = $OrderHelper->orderBack($oid, $status, $remarks, array('uid' => $this->UserConfig['Id'], 'uname' => $member_info['u_realname']), $v_type);
            if ($result['success']) {
                $result1 = $OrderHelper->orderupUpdate(array(
                    'up_status' => '作废'
                ),'',$oid);
                $MemberHelper->saveLog($this->UserConfig, '视频审退回', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                ErrorMsg::Debug('保存成功', true);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;*/
        case 'suspend':
        	$oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) {
                ErrorMsg::Debug('无权操作');
            }
            $this->Assign('oid', $oid);
            echo $this->GetView('order_fvosuspend.php');
            break;
        case 'suspendsave':
        	$oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            $remarks = $this->Param['remarks'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if (!$order_check['success']) {
                ErrorMsg::Debug('无权操作');
            }
            $result = $OrderHelper->orderSuspend($oid, $remarks, array('uid' => $this->UserConfig['Id'], 'uname' => $member_info['u_realname']), 'fvo');
            if ($result['success']) {
                $MemberHelper->saveLog($this->UserConfig, '视频审暂停', json_encode($this->Param), $this->Controller . '_' . $this->Action);
                $suspendMsg = $OrderHelper->suspendMsg($oid);
                if(!empty($suspendMsg))  $MessageHelper->sendSingle($suspendMsg);
                ErrorMsg::Debug('保存成功', true);
            } else {
                ErrorMsg::Debug($result['msg']);
            }
            break;
        default:
    }
}